//
//  Today_todayContentCollectionViewCell.m
//  Time
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "Today_todayContentCollectionViewCell.h"
#import "Today_todayContentTableViewCell.h"
#import "SCBShuffView.h"
#import "Today_today_Sympo_Cell.h"
#import "Today_today_Ting_Cell.h"
#import "Today_today_Slice_Cell.h"
#import "Today_today_Debris_Cell.h"
#import "Today_today_Music_Cell.h"
#import "Today_today_Visual_Cell.h"
#import "SCB_Today_TodayContentModel.h"


@interface Today_todayContentCollectionViewCell ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic,retain) UITableView *todayContentTableView;
@property (nonatomic,assign) BOOL isDown;

@end


@implementation Today_todayContentCollectionViewCell
-(void)dealloc{
    [_todayContentTableView release];
    [_modelArray release];
    _todayContentTableView.delegate = nil;
    _todayContentTableView.dataSource = nil;
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.modelArray = [NSMutableArray arrayWithCapacity:5];

        
        self.todayContentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - 88) style:UITableViewStyleGrouped];
//        self.todayContentTableView.backgroundColor = [UIColor grayColor];
        self.todayContentTableView.delegate = self;
        self.todayContentTableView.dataSource = self;
        
        /**
         轮播图
         
         - returns: 给页面添加一个轮播图
         */
        SCBShuffView *shuffView = [[SCBShuffView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT * 0.3)];
        self.todayContentTableView.tableHeaderView = shuffView;
        self.todayContentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.contentView addSubview:self.todayContentTableView];
        
        
        
        self.todayContentTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.isDown = YES;
                
            [self.delegate loadNewData:self.isDown];
            [self.todayContentTableView.mj_header endRefreshing];
        
        }];
        
        self.todayContentTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            self.isDown = NO;
            
            [self.delegate loadNewData:self.isDown];
            [self.todayContentTableView.mj_footer endRefreshing];
        }];
        
        
        
        /**
         *  重用池注册(一堆)
         */
        [self.todayContentTableView registerClass:[Today_todayContentTableViewCell class] forCellReuseIdentifier:@"today_today_contentTableViewCell"];
        
        
        [self.todayContentTableView registerClass:[Today_today_Ting_Cell class] forCellReuseIdentifier:@"today_today_Ting_Cell"];
        
        [self.todayContentTableView registerClass:[Today_today_Sympo_Cell class] forCellReuseIdentifier:@"today_today_Sympo_Cell"];
        
        [self.todayContentTableView registerClass:[Today_today_Slice_Cell class] forCellReuseIdentifier:@"today_today_Slice_Cell"];
        
        [self.todayContentTableView registerClass:[Today_today_Debris_Cell class] forCellReuseIdentifier:@"today_today_Debris_Cell"];
        
        [self.todayContentTableView registerClass:[Today_today_Music_Cell class] forCellReuseIdentifier:@"today_today_Music_Cell"];
        
        [self.todayContentTableView registerClass:[Today_today_Visual_Cell class] forCellReuseIdentifier:@"today_today_Visual_Cell"];
        
        [_todayContentTableView release];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    
    
}

//TableView协议方法.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCB_Today_TodayContentModel *model = [self.modelArray objectAtIndex:indexPath.section];
    NSString *typeName = model.typeName;
    if ([typeName isEqualToString:@"Ting"]) {
        Today_today_Ting_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Ting_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else if ([typeName isEqualToString:@"坐谈会"]) {
        Today_today_Sympo_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Sympo_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else if ([typeName isEqualToString:@"片刊"]){
        Today_today_Slice_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Slice_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else if ([typeName isEqualToString:@"碎片"]){
        Today_today_Debris_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Debris_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else if ([typeName isEqualToString:@"唱片馆"]){
        Today_today_Music_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Music_Cell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else if ([typeName isEqualToString:@"视觉系"]){
        Today_today_Visual_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_Visual_Cell"];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    } else {
        Today_todayContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"today_today_contentTableViewCell" forIndexPath:indexPath];
        cell.model = [self.modelArray objectAtIndex:indexPath.section];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCB_Today_TodayContentModel *model = [self.modelArray objectAtIndex:indexPath.section];
    NSString *typeName = model.typeName;
    
    if ([typeName isEqualToString:@"Ting"]) {
        return 390 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.tingContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
    } else if ([typeName isEqualToString:@"片刊"]) {
        return 390 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.tingContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
    } else if ([typeName isEqualToString:@"碎片"]) {
        return SWIDTH - 180 - 20 + 300 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.todayContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
    } else if ([typeName isEqualToString:@"唱片馆"]) {
        return 270 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.todayContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
    } else if ([typeName isEqualToString:@"视觉系"]) {
        return SWIDTH - 180 - 20 + 300 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.todayContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
    }
    return 300 + 20 + [Tools getTextHeight:model.todayContent.text withWidth:SWIDTH - 20 withFontSize:15.0] + [Tools getTextHeight:model.todayContent.title withWidth:SWIDTH - 20 withFontSize:17.0];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SCB_Today_TodayContentModel *model = [self.modelArray objectAtIndex:indexPath.section];
    NSString *typeName = model.typeName;
    if ([typeName isEqualToString:@"唱片馆"]) {
#warning push to musicVC;
        [self.delegate pushToMusic:model.todayContent.contentId];
    } else {
//        SCB_Today_TodayContentModel *model = [self.modelArray objectAtIndex:indexPath.section];
        [self.delegate pushToDetail:model.todayContent.contentId];
    }
}

-(void)setModelArray:(NSMutableArray *)modelArray{
    if (_modelArray != modelArray) {
        [_modelArray release];
        _modelArray = [modelArray retain];
    }
    [self.todayContentTableView reloadData];
}


@end
